import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { GeneralToolServiceProxy, GeneralToolEditDto, ComboboxItemDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

import * as moment from 'moment';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'createOrEditGeneralToolModal',
  templateUrl: './create-or-edit-general-tool-modal.component.html'
})

export class CreateOrEditGeneralToolModalComponent extends AppComponentBase {

  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('dueDatePicker') dueDatePicker: ElementRef;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  generalTool = new GeneralToolEditDto();

  categorys = new Array<ComboboxItemDto>();
  classifications = new Array<ComboboxItemDto>();
  es: any;
  verificationDate: Date;
  nextVerificationDate: Date;
  intoStockDate: Date;

  constructor(
    injector: Injector,
    private _generalToolService: GeneralToolServiceProxy
  ) {
    super(injector);
  }
  ngOnInit() {
    this.es = {
      firstDayOfWeek: 1,
      dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
      monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
      today: 'Hoy',
      clear: 'Borrar',

      dayNames: ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"],
      dayNamesShort: ["dom", "lun", "mar", "mié", "jue", "vie", "sáb"],


      monthNamesShort: ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"],

    };
  }

  show(id?: string): void {
    this.active = true;

    this._generalToolService.getGeneralToolForEdit(id).subscribe(result => {
      this.generalTool = result.generalTool;

      // tslint:disable-next-line:curly
      this.verificationDate = this.generalTool.verificationDate ? this.generalTool.verificationDate.year() === 1 ? undefined : this.generalTool.verificationDate.toDate() : undefined;
      this.nextVerificationDate = this.generalTool.nextVerificationDate ? this.generalTool.nextVerificationDate.year() === 1 ? undefined : this.generalTool.nextVerificationDate.toDate() : undefined;
      this.intoStockDate = this.generalTool.intoStockDate ? this.generalTool.intoStockDate.year() === 1 ? undefined : this.generalTool.intoStockDate.toDate() : undefined;

      this.categorys = result.categorys;
      this.classifications = result.classifications;

      this.modal.show();
    });
  }

  save(): void {
    this.saving = true;

    this.generalTool.verificationDate = this.verificationDate ? moment(this.verificationDate).add(8, 'hour') : undefined;
    this.generalTool.nextVerificationDate = this.verificationDate ? moment(this.nextVerificationDate).add(8, 'hour') : undefined;
    this.generalTool.intoStockDate = this.verificationDate ? moment(this.intoStockDate).add(8, 'hour') : undefined;

    this._generalToolService.createOrUpdateGeneralTool(this.generalTool)
      .pipe(finalize(() => { this.saving = false; }))
      .subscribe(() => {
        this.notify.info(this.l('SavedSuccessfully'));
        this.close();
        this.modalSave.emit(null);
      });
  }

  close(): void {
    this.active = false;
    this.modal.hide();
  }
}
